package cn.itcast.service.company.impl;

import cn.itcast.dao.company.CompanyDao;
import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyDao companyDao;

    @Override
    public List<Company> findAll() {
        return companyDao.findAll();
    }

    @Override
    public void save(Company company) {
        companyDao.save(company);
    }

    @Override
    public Company findById(String id) {
        return companyDao.findById(id);
    }

    @Override
    public void update(Company company) {
        companyDao.updateById(company);
    }

    @Override
    public void delete(String id) {
        companyDao.deleteById(id);
    }


    public PageInfo findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Company> list = companyDao.findAll();
        return new PageInfo<Company>(list,5);  //PageInfo中有PageBean中的所有的属性，可以使用PageInfo替代PageBean
    }

    @Override
    public List<String> findModuleLisByCompanyId(String companyId) {
//        select  module_id  from  ss_company_module where conpany_id=?
        return companyDao.findModuleLisByCompanyId(companyId);
    }

    @Override
    public void updateCompanyModule(String companyId, String moduleIds) {
//        先清空此企业的菜单权限数据
//     delete from ss_company_module where company_id=#{companyId}
        companyDao.deleteCompanyAndModuleByCompanyId(companyId);
//                然后在插入
        String[] mIds = moduleIds.split(",");
        for (String mId : mIds) {
            companyDao.saveCompanyAndModule(companyId,mId);
//        insert into ss_company_module (company_id,module_id) values (#{companyId},#{moduleId})
        }


    }

   /* @Override
    public PageBean findPage(int pageNum, int pageSize) {
//        起始位置=（当前页-1）*每页显示条数=（pageNum-1）*pageSize
//        list:当前页的数据  select * from ss_company limit 起始位置,每页显示的条数
//        total: select count(0) from ss_company
        List list = companyDao.findPage((pageNum-1)*pageSize,pageSize);
        Long total = companyDao.findCount();
        return new PageBean(pageNum,pageSize,list,total);
    }*/
}
